package pogvue.gui;

import pogvue.analysis.AAFrequency;
import pogvue.analysis.AlignmentUtil;
import pogvue.datamodel.GFF;
import pogvue.datamodel.SequenceI;
import pogvue.gui.schemes.ColourSchemeI;

import java.awt.*;
import java.util.Hashtable;
import java.util.Vector;

public final class BlockRenderer implements RendererI {
    protected final Color   color = Color.black;
    protected final Color   maroon = new Color(200,0,50);
    protected final Color   purple = new Color(255,100,255);

    public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {
	Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);
	return c;
    }
    
    public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText,Vector pid, int seqnum,AlignViewport av, Hashtable props, int intpid[][]) {
	
	int i      = start;
	int length = seq.getLength();

	Color currentColor = Color.white;
	Vector kmers = av.getKmers();

	g.setColor(Color.black);

        int prev = -1;
	
	Vector excl = new Vector();

	for (int ii = 0; ii < av.getAlignment().getHeight(); ii++) {
	    SequenceI iseq = av.getAlignment().getSequenceAt(ii);

	    if (av.getSelection().contains(iseq) || iseq.getName().indexOf("Exon") >= 0 || iseq instanceof GFF) {
		excl.addElement(iseq);
	    }
	}

	int blstart = start - 20;
	int blend   = end   + 20;

	if (blstart < 0) {
	    blstart = 0;
	}

	if (blend > av.getAlignment().getWidth()) {
	    blend = av.getAlignment().getWidth();
	}

	Hashtable blocks = AAFrequency.findBlocks(av.getAlignment().getSequences(),
						  blstart,
						  blend,
						  excl);
	
	Hashtable hkmer = AlignmentUtil.findKmers(seq,blstart,blend,kmers);

	boolean   inblock = false;

	int       bstart = -1;
	int       blen;
	int       klen;
	int       block_end = -1;
	Color bc;

	// System.out.println("Rendering " + start + " " + end);

	while (i <= blend && i < length) {
	    boolean foundblock = false;

	    if (blocks.get(new Integer(i-blstart)) != null) {

		blen   = (Integer) blocks.get(new Integer(i - blstart));
		
		foundblock = true;
		
		block_end = i + blen-1;

		g.setColor(getColor(blen));
		g.fillRect(x1+(int)(width*(i-start)),y1,
			   (int)width+1,             height/2);
	    }

	    boolean foundKmer = false;

	    if (hkmer.get(new Integer(i-blstart)) != null) {


		klen   = (Integer) hkmer.get(new Integer(i - blstart));

		//		System.out.println("Got kmer " + i + " " + start + " kength " + klen);		

		foundKmer = true;

		g.setColor(Color.lightGray);

		if ((int)width> 5) {
		    g.fillRect(x1+(int)(width*(i-start)),y1+height/2,
			       (int)(width*klen)+1,             height/2);
		} else {
		    g.fillRect(x1+(int)(width*(i-start)),y1+height/2,
			       (int)(width*klen)+1,             height/2);

		}

	    }
		       
	    if (width > 5) {
		g.setColor(Color.black);
		g.drawString(seq.getSequence().substring(i,i+1),x1+(int)(width*(i-start)),y1+height);
	    }
	    i++;
	}
    }

    private Color getColor(int blen) {

	Color bc;

	if ((blen+1)%3 == 0) {
	    bc = Color.red;
	} else if (blen%3 == 0) {
	    bc = Color.green;
	} else {
	    bc = Color.orange;
	}
	return bc;
	    //if (blen > 20 ) {
	    //	    bc = Color.yellow;
// 	}else if (blen > 10) {
// 	    bc = Color.red;
// 	} else if (blen > 5) {
// 	    bc = Color.blue;
// 	} else if (blen > 2) {
// 	    bc = purple;
// 	} else if (blen == 2) {
// 	    bc = Color.orange;

// 	} else {
// 	    bc = Color.lightGray;
// 	}
// 	return bc;
    }
}
